/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.controller;

import org.apache.log4j.Logger;
import org.app.service.CrawlsService;
import org.app.service.SearchService;
import org.app.service.impl.CrawlsServiceImpl;
import org.app.service.impl.SearchServiceImpl;
import org.gui.AutoDownloadMovieGUI;

/**
 *
 * @author LamLe
 */
public class SpiderCrawlsAction implements Runnable {
    
    private static Logger logger = Logger.getLogger(SpiderCrawlsAction.class);
    AutoDownloadMovieGUI main;
    SearchService searchProcess;
    CrawlsService crawlsService;
    
    public SpiderCrawlsAction(AutoDownloadMovieGUI main) {
        this.main = main;
    }
    
    @Override
    public void run() {
        try {
            log("Searching : " + main.getMovieName() + "...");
            log("------------------------------------------");
            log("------------------------------------------");

            // init search service
            searchProcess = new SearchServiceImpl(main);
            searchProcess.searchMovie(main.getMovieName());

            // init crawls service
            crawlsService = new CrawlsServiceImpl(main);
            Thread t = new Thread(crawlsService);
            t.start();
        } catch (Exception e) {
            logger.error("Failed", e);
        }
        
    }
    
    private void log(String str) {
        main.write(str);
    }
}
